//
//  SetViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/5.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SetViewController.h"
#import "SetTableViewCell.h"
#import "UIAlertTool.h"
#import "SetDingShiViewController.h"
#import "SetShouCangViewController.h"
#import "Player.h"
#import "SetPhoteTableViewCell.h"
#import "myViewController.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)UIImageView *imageV;
@property (nonatomic ,strong)NSUserDefaults *user;
@property (nonatomic ,strong)NSString *str;

@end

@implementation SetViewController
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    
//    self.imageV=[[UIImageView alloc]initWithFrame:self.view.bounds];
//
//     self.imageV.image=[UIImage imageNamed:@"b364d3c98949f26d110ae73b5b01e1a2.jpg"];
//    self.imageV.userInteractionEnabled=YES;
//    
//    [self.view addSubview:self.imageV];
    
    self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.backgroundView=self.imageV;
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    [self.view addSubview:self.tableView];
//    
//    UISwitch *switch1=[[UISwitch alloc]initWithFrame:CGRectMake(FM_Screen_Width-FM_Screen_Pts_Width*60, FM_Screen_Pts_Height*380, FM_Screen_Pts_Width*50, FM_Screen_Pts_Height*30)];
//    [switch1 addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.tableView addSubview:switch1];
//    NSUserDefaults *de=[NSUserDefaults standardUserDefaults];
//    
//    if ([de objectForKey:@"play"]==nil) {
//        [switch1 setOn:NO];
//        
//    }else{
//        
//        [switch1 setOn:YES];
//    }

    
}
//section 分区间隔
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0;
    }else if(section==1){
        return 0;
    }else{
        return FM_Screen_Pts_Height*15;
    }
    
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return FM_Screen_Pts_Height*300;
        
    }else{
        return FM_Screen_Pts_Height*55;
    }
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else if (section==1){
        return 1;
    }else if (section ==2){
        return 1;
    }else if (section ==3){
        return 1;
    }else if (section ==4){
        return 1;
    }else if (section ==5){
        return 1;
    }
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        SetTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"huancun"];
        if (!cell) {
            cell=[[SetTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"huancun"];
        }
        
        cell.lable1.text=@"清除缓存";
        cell.imageV1.image=[UIImage imageNamed:@"cell_delete_h@2x.png"];
        float num = [[SDImageCache sharedImageCache]getSize];
        NSString *str = [NSString stringWithFormat:@"%.2f M",num/1024/1000];
        cell.lable2.text=str;
       
        return cell;
    }else if (indexPath.section==2){
       
            SetTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"wangluo"];
        
            if (!cell) {
                cell=[[SetTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"wangluo"];
            }
            cell.lable1.text=@"仅wife下播放";
        cell.imageV1.image=[UIImage imageNamed:@"me_setting_favAlbum@2x.png"];
        
        
       
        return cell;
    }else if (indexPath.section==5){
        
        SetTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"huancun1"];
        if (!cell) {
            cell=[[SetTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"huancun1"];
        }
        
        cell.lable1.text=@"关于";
        cell.imageV1.image=[UIImage imageNamed:@"setting_sm_icon@2x.png"];

        
        
        return cell;
    }else if (indexPath.section==4){
        SetTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"huancun2"];
        if (!cell) {
            cell=[[SetTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"huancun2"];
        }
        
        cell.lable1.text=@"定时关闭";
        cell.imageV1.image=[UIImage imageNamed:@"me_setting_clock@2x.png"];
        
        return cell;
    }else if (indexPath.section==3){
        SetTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"huancun3"];
        if (!cell) {
            cell=[[SetTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"huancun3"];
        }
        
        cell.lable1.text=@"我的收藏";
        cell.imageV1.image=[UIImage imageNamed:@"me_setting_liked@2x.png"];
        
        return cell;

    }else if (indexPath.section==0){
        SetPhoteTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"huancun4"];
        if (!cell) {
            cell=[[SetPhoteTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"huancun4"];
        }

        cell.imageV.image=[UIImage imageNamed:@"find_radio_default@2x.png"];
        
        return cell;
    }
   
    
    
    return NULL;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        [self.tableView reloadData];
        float num = [[SDImageCache sharedImageCache]getSize];
        self.str = [NSString stringWithFormat:@"确定清楚缓存 %.2f M",num/1024/1000];
        UIAlertTool *alert =[[UIAlertTool alloc]init];
      
        [alert showAlertView:self title:@"警告" message:self.str cancelButtonTitle:@"取消" otherButtonTitle:@"确定" confirm:^{
            NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSFileManager *manager = [NSFileManager defaultManager];
            NSError *error = nil;
            
            [manager removeItemAtPath:cachesPath error:&error];
            BOOL fileBool = [manager fileExistsAtPath:cachesPath];
        
            self.str=@"0.0M";
            [self.tableView reloadData];
            if (error == nil) {
                
            } else if (fileBool == YES) {
                NSLog(@"%@", error);
                
            }

        } cancle:^{
           
        }];
    
    }else if (indexPath.section==3){
        SetShouCangViewController *shou=[[SetShouCangViewController alloc]init];
        
        [self.navigationController pushViewController:shou animated:YES];
      
    }else if (indexPath.section==4){
        SetDingShiViewController *ding=[[SetDingShiViewController alloc]init];
        
        [self.navigationController pushViewController:ding animated:YES];
    }else if(indexPath.section==2){
         UIAlertTool *alert1 =[[UIAlertTool alloc]init];
        [alert1 showAlertView:self title:@"警告" message:@"该功能暂未开放" cancelButtonTitle:@"确定" otherButtonTitle:@"取消" confirm:^{
            
        } cancle:^{
            
        }];   }
            
    else if(indexPath.section == 5){
        myViewController *my=[[myViewController alloc]init];
        
        [self.navigationController pushViewController:my animated:YES];
       
    }
    
}
//通知放不放
-(void)switchClick:(UISwitch *)sw
{
    NSUserDefaults *de=[NSUserDefaults standardUserDefaults];
    
    if (sw.on) {
        
        // 开启网络监听器
        AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
        [afNetworkReachabilityManager startMonitoring];
//        [[NSUserDefaults standardUserDefaults] setObject:@"on" forKey:@"bofang"];
        [de setObject:@"asd" forKey:@"play"];
        [de synchronize];
        [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            
            switch (status) {
                case AFNetworkReachabilityStatusNotReachable:
                    NSLog(@"无网络");
      
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    NSLog(@"WIFI网络");
                    [self playno];
                     break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    NSLog(@"蜂窝数据");
                    
                    
                    break;
                case AFNetworkReachabilityStatusUnknown:
                    NSLog(@"未知网络类型");
                  
                    break;
                    
                    
                default:
                    break;
            }
            
        }];
        
        
  }else{
      
//      [self playyes];
      [[NSUserDefaults standardUserDefaults] setObject:@"off" forKey:@"bofang"];
      [de setObject:nil forKey:@"play"];
    }
    
}
////不播放
-(void)playno
{
    NSUserDefaults *sur=[NSUserDefaults standardUserDefaults];
    if ([sur objectForKey:@"bofang"]==nil) {
       
        [sur setObject:@"off" forKey:@"bofang"];
        
    }else{
        
        [sur setObject:@"on" forKey:@"bofang"];
        
    }
    [sur synchronize];
    
    
    
}
//{
//    Player *player = [Player shareInstance];
//    [player.audioPlayer stop];
//
//}
////不播放
//-(void)playno
//{
//
//}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (section==0) {
//        UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*300)];
//        imageV.image=[UIImage imageNamed:@"b364d3c98949f26d110ae73b5b01e1a2.jpg"];
//        
//        
//        return imageV;
//
//    }
//    return nil;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
